package com.xin.ruanjian.omsystem.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.activity.LoginActivity;
import com.xin.ruanjian.omsystem.activity.ShopDetailActivity;
import com.xin.ruanjian.omsystem.app.MyApplication;
import com.xin.ruanjian.omsystem.bean.IndexBean;
import com.youth.banner.Banner;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by local123 on 2018/2/3.
 */

public class MainRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private IndexBean data;

    public MainRvAdapter(Context context) {
        this.context = context;
    }

    public void setData(IndexBean data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void addData(List<IndexBean.MsgBean.ShopBean> add) {
        if (data.getMsg().getShop() != null && add != null && !add.isEmpty()) {
            data.getMsg().getShop().addAll(add);
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new BannerHolder(LayoutInflater.from(context).inflate(R.layout.item_rv_main_bg, parent, false));
        } else if (viewType == 1) {
            return new TitleViewHolder(LayoutInflater.from(context).inflate(R.layout.item_rv_main_title, parent, false));
        } else {
            return new ShopHolder(LayoutInflater.from(context).inflate(R.layout.item_rv_main_shop, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (position == 0) {

        } else if (position > 1) {
            ShopHolder shopHolder = (ShopHolder) holder;
            shopHolder.tvShopName.setText(data.getMsg().getShop().get(position - 2).getShopname());
            shopHolder.tvShopDistance.setText(data.getMsg().getShop().get(position - 2).getDistance());
            shopHolder.llShopStars.removeAllViews();
            for (int i = 0; i < data.getMsg().getShop().get(position - 2).getShopstars(); i++) {
                ImageView iv = new ImageView(context);
                if (i < data.getMsg().getShop().get(position - 2).getShopstars()) {
                    iv.setImageResource(R.mipmap.home_icon_1);
                }else {
                    iv.setImageResource(R.mipmap.home_icon_1_gray);
                }
                shopHolder.llShopStars.addView(iv);
            }
            shopHolder.llShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (MyApplication.getLogin() == null) {
                        context.startActivity(new Intent(context, LoginActivity.class));
                        return;
                    }
                    context.startActivity(new Intent(context, ShopDetailActivity.class)
                    .putExtra("shop_info", data.getMsg().getShop().get(position - 2)));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (data != null && data.getMsg().getShop() != null) {
            return data.getMsg().getShop().size() + 2;
        }
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }


    class ShopHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_shop_pic)
        ImageView ivShopPic;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_shop_distance)
        TextView tvShopDistance;
        @BindView(R.id.tv_shop_month_sale)
        TextView tvShopMonthSale;
        @BindView(R.id.ll_shop_stars)
        LinearLayout llShopStars;
        @BindView(R.id.ll_shop)
        LinearLayout llShop;

        ShopHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class BannerHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.bga_pic_ecoinmanage)
        Banner bgaPicEcoinmanage;

        BannerHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class TitleViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;

        TitleViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
